package com.arch.flink.window;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author pizhihui
 * @date 2024-03-11 11:19
 */
public class Demo2WindowProcessFunction {


    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


        DataStreamSource<String> source = env.socketTextStream("127.0.0.1", 9999);

        SingleOutputStreamOperator<Tuple2<String, Integer>> res = source.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
                        String[] split = s.split(",");
                        for (String s1 : split) {
                            collector.collect(Tuple2.of(s1, 1));
                        }
                    }
                })
                .keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
                    @Override
                    public String getKey(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
                        return stringIntegerTuple2.f0;
                    }
                })
                // .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                .window(SlidingProcessingTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                //.timeWindow(Time.seconds(10), Time.seconds(5))
                .process(new ProcessWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {

                    FastDateFormat dataFormat = FastDateFormat.getInstance("HH:mm:ss");
                    @Override
                    public void process(String o,
                                        ProcessWindowFunction<Tuple2<String, Integer>,
                                                Tuple2<String, Integer>, String, TimeWindow>.Context context,
                                        Iterable<Tuple2<String, Integer>> elements,
                                        Collector<Tuple2<String, Integer>> out) throws Exception {


                        long now = System.currentTimeMillis();
                        long l = context.currentProcessingTime();
                        long l1 = context.currentWatermark();
                        long start = context.window().getStart();
                        long end = context.window().getEnd();


                        System.out.println(
                                "当前时间=" + dataFormat.format(now) + "," +
                                "processing time=" + dataFormat.format(l) + "," +
                                "watermark=" + dataFormat.format(l1) + "," +
                                "start=" + dataFormat.format(start) + ",end=" + dataFormat.format(end));

                        int sum = 0;
                        for (Tuple2<String, Integer> element : elements) {
                            sum += element.f1;
                        }
                        out.collect(Tuple2.of(o, sum));
                    }
                });


        res.print().setParallelism(1);

        env.execute("Socket WordCount");

    }

}
